package com.yunhe.abnormal.controller.base;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.base.AlarmNotifyConfigDTO;
import com.yunhe.common.model.base.AlarmSmsConfigDTO;
import com.yunhe.abnormal.service.base.AlarmNotifyConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 异常通知配置Controller
 * @author liuronglei
 */
@Api(value = "alarmNotifyConfig", description = "异常通知配置", tags = {"Base_AlarmNotifyConfig"})
@RestController
@RequestMapping("/alarmNotifyConfigs")
public class AlarmNotifyConfigController {
    @Autowired
    private AlarmNotifyConfigService alarmNotifyConfigService;

    /**
     * 新增异常通知配置对象
     * @param alarmNotifyConfig 异常通知配置对象
     */
    @ApiOperation("根据异常通知配置ID获得异常通知配置")
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<ResultObject> create(@RequestBody AlarmNotifyConfigDTO alarmNotifyConfig)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        AlarmNotifyConfigDTO newAlarmNotifyConfig = alarmNotifyConfigService.create(alarmNotifyConfig);
        return new ResponseEntity<>(new ResultObject<>(newAlarmNotifyConfig), HttpStatus.OK);
    }

    /**
     * 根据异常通知配置ID获得异常通知配置
     * @param id 异常通知配置ID
     */
    @ApiOperation("根据异常通知配置ID获得异常通知配置")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> delete(@PathVariable("id") Long id) {
        alarmNotifyConfigService.deleteById(id);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    /**
     * 根据异常通知配置ID获得异常通知配置
     * @param ids 异常通知配置ID
     */
    @ApiOperation("根据异常通知配置ID获得异常通知配置")
    @ApiImplicitParam(name = "ids", value = "异常通知配置ID", required = true, dataType = "long", paramType = "query")
    @RequestMapping(value = "/batch", method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> batchDelete(@RequestParam("ids") List<Long> ids) {
        alarmNotifyConfigService.batchDelete(ids);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    /**
     * 更新异常通知配置状态
     * @param id 对象ID
     * @param status 状态
     */
    @ApiOperation("更新异常通知配置状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "异常通知配置ID", required = true, dataType = "long", paramType = "path"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/{id}/status", method = RequestMethod.PUT)
    public ResponseEntity<ResultObject> updateStatus(@PathVariable("id") Long id,
                                                     @RequestParam("status") Integer status)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        AlarmNotifyConfigDTO newAlarmNotifyConfig = alarmNotifyConfigService.updateStatus(id, status);
        return new ResponseEntity<>(new ResultObject<>(newAlarmNotifyConfig), HttpStatus.OK);
    }

    /**
     * 更新异常通知配置是否全部电站
     * @param id 对象ID
     * @param isAllStation 状态
     */
    @ApiOperation("更新异常通知配置是否全部电站")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "异常通知配置ID", required = true, dataType = "long", paramType = "path"),
            @ApiImplicitParam(name = "isAllStation", value = "是否全部电站", dataType = "boolean", paramType = "query")
    })
    @RequestMapping(value = "/{id}/isAllStation", method = RequestMethod.PUT)
    public ResponseEntity<ResultObject> updateIsAllStation(@PathVariable("id") Long id,
                                                           @RequestParam("isAllStation") Boolean isAllStation)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        AlarmNotifyConfigDTO newAlarmNotifyConfig = alarmNotifyConfigService.updateIsAllStation(id, isAllStation);
        return new ResponseEntity<>(new ResultObject<>(newAlarmNotifyConfig), HttpStatus.OK);
    }

    /**
     * 更新异常通知配置电站
     * @param id 对象ID
     * @param stations 适用电站ID列表
     */
    @ApiOperation("更新异常通知配置电站")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "异常通知配置ID", required = true, dataType = "long", paramType = "path"),
            @ApiImplicitParam(name = "stations", value = "适用电站ID列表", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/{id}/stations", method = RequestMethod.PUT)
    public ResponseEntity<ResultObject> updateStation(@PathVariable("id") Long id,
                                                      @RequestParam("stations") Optional<List<Long>> stations)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        AlarmNotifyConfigDTO newAlarmNotifyConfig = alarmNotifyConfigService.updateStation(id, stations.orElse(null));
        return new ResponseEntity<>(new ResultObject<>(newAlarmNotifyConfig), HttpStatus.OK);
    }

    /**
     * 根据异常通知配置ID获得异常通知配置
     * @param id 异常通知配置ID
     */
    @ApiOperation("根据异常通知配置ID获得异常通知配置")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> get(@PathVariable("id") Long id) {
        AlarmNotifyConfigDTO alarmNotifyConfig = alarmNotifyConfigService.findById(id);
        return new ResponseEntity<>(new ResultObject<>(alarmNotifyConfig), HttpStatus.OK);
    }

    /**
     * 根据条件获得异常通知配置列表
     * @param type 类型
     * @param alarmLevelId 异常级别ID
     * @param alarmNotifyTypeName 异常通知类型name
     * @param firmId 单位ID
     * @param userId 用户ID
     */
    @ApiOperation("根据条件获得异常通知配置列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "alarmLevelId", value = "异常级别ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "alarmNotifyTypeName", value = "异常通知类型name", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "firmId", value = "单位ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "long", paramType = "query")
    })
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<ResultObject> search(@RequestParam("type") Integer type,
                                               @RequestParam("alarmLevelId") Optional<Long> alarmLevelId,
                                               @RequestParam("dtime") Optional<String> alarmNotifyTypeName,
                                               @RequestParam("firmId") Optional<Long> firmId,
                                               @RequestParam("userId") Optional<Long> userId) {
        Iterable<AlarmNotifyConfigDTO> results = alarmNotifyConfigService.search(type, alarmLevelId.orElse(null),
                alarmNotifyTypeName.orElse(null), firmId.orElse(null), userId.orElse(null));
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    /**
     * 根据异常通知配置ID获得异常短信接收人配置
     * @param id 异常通知配置ID
     */
    @ApiOperation("根据异常通知配置ID获得异常短信接收人配置")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long", paramType = "path")
    @RequestMapping(value = "/{id}/alarmSmsConfig", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchSmsConfig(@PathVariable("id") Long id) {
        Iterable<AlarmSmsConfigDTO> results = alarmNotifyConfigService.searchSmsConfig(id);
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    /**
     * 更新异常短信接收人配置
     * @param id 异常短信接收人配置ID
     * @param alarmSmsConfigs 异常短信接收人配置
     */
    @RequestMapping(value = "/{id}/alarmSmsConfig", method = RequestMethod.PUT)
    @ApiOperation("更新异常短信接收人配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "异常通知配置ID", required = true, dataType = "long", paramType = "path")
    })
    public ResponseEntity update(@PathVariable("id") Long id,
                                 @RequestBody List<AlarmSmsConfigDTO> alarmSmsConfigs)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        alarmNotifyConfigService.updateSmsConfig(id, alarmSmsConfigs);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }
}
